$(function () {
    layui.use('table', function () {
        const table = layui.table;

        const tableObj = table.render({
            elem: '#point-table',
            url: '/KnowledgeManager/getKnowledgeList?' + $("#searchform").serialize(), //数据接口
            page: true,
            method: 'post',
            contentType: 'application/json;charset=utf-8',
            cols: [
                [
                    {type: 'checkbox', fixed: 'left', width: 50},
                    {field: 'name', title: '知识点名称', width: '45%'},
                    {field: 'addName', title: '创建人', width: '10%'},
                    {
                        field: 'addDate',
                        title: '创建时间',
                        width: '10%',
                        templet: function (d) {
                            return getTimeStr(d.addDate)
                        }
                    },
                    {field: 'modifyName', title: '最后修改人', width: '10%'},
                    {
                        field: 'modifyDate', title: '最后修改时间', width: '10%',
                        templet: function (d) {
                            return getTimeStr(d.modifyDate)
                        }
                    },
                    {
                        field: 'state', title: '状态', width: '10%',
                        templet: function (d) {
                            return d.state === 'enabled' ? '启用' : '停用'
                        }
                    },
                ],
            ],
            parseData: parseLimitResult,
        });

        function getTimeStr(data) {
            return String(data.year + 1900) + '/' + data.month + '/' + data.date + ' ' + data.hours + ':' + data.minutes + ':' + data.seconds
        }

        function reload(search) {
            tableObj.reload({
                url: '/KnowledgeManager/getKnowledgeList?' + $("#searchform").serialize(), //数据接口
                page: true,
                method: 'post',
                contentType: 'application/json;charset=utf-8',
                where: {
                    search: search
                }
            });
        }

        $('#searchbtn').on('click', function () {
            reload($('#searchfrom').serializeArray().reduce(function(total, cur){total[cur.name] = cur.value; return total},{}))
        })

        //知识点添加按钮
        $('#knowledge_add').on('click', function () {
            location = "/KnowledgeManager/addKnowledgeFrame/";
        })
        //知识点编辑按钮
        $('#knowledge_edit').on('click', function () {
            let data = getKnowmanageId();
            if (!data) {
                return;
            }
            location = "/KnowledgeManager/editKnowledgeFrame/?id=" + data.id + "&name=" + data.name;
        })
        //知识点使能/禁用按钮
        $('#knowledge_ena').on('click', function () {
            let data = getKnowmanageId();
            if (!data) {
                return;
            }
            $.ajax({
                url: "/KnowledgeManager/enableKnowledge" + "?id=" + data.id + "&enable=" + data.enable,
                type: "POST",
                success: function () {
                    reload();
                },
            });
        })
        $('#knowledge_del').on('click', function () {
            let data = getKnowmanageId();
            if (!data) {
                return;
            }
            $.ajax({
                url: "/KnowledgeManager/deleteKnowledge?id=" + data.id,
                type: 'post',
                success: function () {
                    reload();
                }
            })
        })
        //查看知识点按键
        $('#knowledge_view').on('click', function () {
            let data = getKnowmanageId()
            if (!data) {
                return;
            }
            layer.open({
                type: 1,
                title: '查看知识点',
                content: $('#view-page'),
                offset: '30px',
                area: ['700px', '465px']
            })

            $('#seeknowledgename').html(data.name)

            table.render({
                elem: '#view-details-table',
                url: '/KnowledgeManager/seeKnowledge', //数据接口
                method: 'post',
                data: data,
                contentType: 'application/json;charset=utf-8',
                page: true,
                cols: [
                    [
                        {field: 'name', title: '岗位名称', width: '15%'},
                        {field: 'singlescount', title: '单项选择题', width: '15%'},
                        {field: 'indefiscount', title: '不定项选择题', width: '15%'},
                        {field: 'fillscount', title: '填空题', width: '15%'},
                        {field: 'judgescount', title: '判断正误题', width: '15%'},
                        {field: 'answerscount', title: '简答题', width: '15%'},
                        {field: 'synthscount', title: '综合题', width: '10%'},
                    ],
                ],
                where: {
                    search: {
                        'knowManagerId': data.id,
                    }
                },
                parseData: parseLimitResult,
            })
        })

        function getKnowmanageId() {
            let data = table.checkStatus('point-table').data;
            if (data.length !== 1) {
                layer.msg('请选择一个知识点');
                return null;
            }
            return {'id': data[0].id, 'enable': data[0].state === 'disabled', 'name': data[0].name};
        }

        function parseLimitResult(value) {
            return {
                "code": value.status - 1,
                "msg": value.msg,
                "count": value.page.total,
                "data": value.data,
            }
        }
    })
})
